package com.test.p01singleton.example06;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Constructor;

public class Client {
    public static void main(String[] args) throws Exception {
        Singleton06 instance01 = Singleton06.getInstance();
        Singleton06 instance02 = Singleton06.getInstance();
        System.out.println(instance01 == instance02);

        //通过反射的方式直接调用私有构造器
//        Class<Singleton06> clazz = (Class<Singleton06>) Class.forName("com.test.p01singleton.example06.Singleton06");
//        Constructor<Singleton06> c = clazz.getDeclaredConstructor(null);
//        c.setAccessible(true);
//        Singleton06 instance03 = c.newInstance();
//        Singleton06 instance04 = c.newInstance();
//        System.out.println(instance03);
//        System.out.println(instance04);

        //反序列化方式
        FileOutputStream fos = new FileOutputStream("D:/text.obj");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(instance01);
        oos.close();
        fos.close();


        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:/text.obj"));
        Singleton06 s5 = (Singleton06)ois.readObject();
        System.out.println(instance01);
        System.out.println(s5);
    }

}
